﻿/// <reference path="functions.js" />

$(function ()
    {
    var my = {};

        my.vm = function () {
            var menuSelected = "#menu-dashboard",
                Items = ko.observableArray([]),
                Templates = ko.observableArray([]);
            var gotoPage = function (selected) {
                menuSelected = selected;
                
                switch (selected)
                {
                    case 'profile':
                        resetUI();
                        jQuery('#nav-profile').addClass('active');
                        jQuery('#profile').show(1000);
                        break;
                    case 'password':
                        resetUI();
                        jQuery('#nav-password').addClass('active');
                        jQuery('#password').show(1000);
                        break;
                    case 'projects':
                        resetUI();
                        jQuery('#nav-projects').addClass('active');
                        jQuery('#projects').show(1000);
                        break;
                    case 'templates':
                        resetUI();
                        jQuery('#nav-templates').addClass('active');
                        jQuery('#templates').show(1000);
                        break;
                    case 'projects':
                    default:
                        resetUI();
                        jQuery('#menu-dashboard').addClass('active');
                        jQuery('#dashboard').show(1000);
                    break;

                }
            }

            var resetUI = function () {
                jQuery('#profile').hide();
                jQuery('#password').hide();
                jQuery('#projects').hide();
                jQuery('#templates').hide();
                jQuery('#dashboard').hide();
                jQuery('#menu-profile').removeClass('active');
                jQuery('#menu-password').removeClass('active');
                jQuery('#menu-projects').removeClass('active');
                jQuery('#menu-templates').removeClass('active');
                jQuery('#menu-dashboard').removeClass('active');
            }

            var uiInit = function () {
                var param = jQuery.QueryString['tab'];
                gotoPage(param);
            },
            initData = function () {
                $.ajax({
                    url: '/account/ManageInitializeJSON',
                    type: 'GET',
                    dataType: 'json',
                    async: false,
                    cache: false,
                    success: function (response) {
                        ko.mapping.fromJS(response.Items, {}, Items);
                        ko.mapping.fromJS(response.Templates, {} , Templates);
                    }
                });
            };
            return {
                gotoPage: gotoPage,
                uiInit: uiInit,
                initData: initData,
                Items: Items,
                Templates: Templates
            };
        }();
        
        my.vm.initData();
        ko.applyBindings(my.vm);
        my.vm.uiInit();
    }
);